import { Column, Entity, Index, JoinColumn, ManyToOne } from 'typeorm';
import { User } from './User';
import { Role } from './Role';

@Index('role_id', ['roleId'], {})
@Index('user_id', ['userId'], {})
@Entity('user_role', { schema: 'blog_back' })
export class UserRole {
  @Column('int', {
    primary: true,
    name: 'user_id',
    comment: '用户外键id',
    unsigned: true,
  })
  userId: number;

  @Column('int', {
    primary: true,
    name: 'role_id',
    comment: '角色外键id',
    unsigned: true,
  })
  roleId: number;

  @Column('timestamp', {
    name: 'create_time',
    comment: '创建时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  createTime: Date;

  @Column('timestamp', {
    name: 'update_time',
    comment: '更新时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  updateTime: Date;

  @ManyToOne(() => User, (user) => user.userRoles, {
    onDelete: 'NO ACTION',
    onUpdate: 'NO ACTION',
  })
  @JoinColumn([{ name: 'user_id', referencedColumnName: 'id' }])
  user: User;

  @ManyToOne(() => Role, (role) => role.userRoles, {
    onDelete: 'NO ACTION',
    onUpdate: 'NO ACTION',
  })
  @JoinColumn([{ name: 'role_id', referencedColumnName: 'id' }])
  role: Role;
}
